<?php
/**
 * Created by PhpStorm.
 * User: william
 * Date: 16-8-31
 * Time: 下午2:44
 */
namespace V4;
class C_ProductTags extends \ResourceClass {
    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require user id ';
        }
        if (!isset($req['userType'])) {
            return 'require user type';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }
        if (!isset($req['enterpriseId'])) {
            return 'require enterprise id';
        }
        if (\Biz\UserHandler::checkAuth($req['userType'],$req['uid'],$req['enterpriseId'],$req['t'],$req['k']) == false) {
            return 'data illegal';
        }

        $product_biz = new \Biz\ProductTagsHandler();
        $tags = $product_biz->getTagsByEnterpriseId($req['enterpriseId']);
        $result['tags'] = $this->pieceTags($tags);
        /*$result['tags'] = array(
            '0' => array(
                'id' => 1,
                'parent_id' => '0',
                'enterprise_id' => '127',
                'title' => '默认标签组',
                'child_tag' => array(
                    '0' => array(
                        'id' => 2,
                        'parent_id' => '1',
                        'enterprise_id' => '127',
                        'title' => '新品上架',
                    ),
                    '1' => array(
                        'id' => 3,
                        'parent_id' => '1',
                        'enterprise_id' => '127',
                        'title' => '热卖',
                    ),
                    '2' => array(
                        'id' => 4,
                        'parent_id' => '1',
                        'enterprise_id' => '127',
                        'title' => '清仓',
                    ),
                )
            ),
            '1' => array(
                'id' => 5,
                'parent_id' => '0',
                'enterprise_id' => '127',
                'title' => '产地',
                'child_tag' => array(
                    '0' => array(
                        'id' => 6,
                        'parent_id' => '5',
                        'enterprise_id' => '127',
                        'title' => '中华人民共和国上海市',
                    ),
                    '1' => array(
                        'id' => 7,
                        'parent_id' => '5',
                        'enterprise_id' => '127',
                        'title' => '德国',
                    ),
                    '2' => array(
                        'id' => 8,
                        'parent_id' => '5',
                        'enterprise_id' => '127',
                        'title' => '日本',
                    ),
                    '3' => array(
                        'id' => 9,
                        'parent_id' => '5',
                        'enterprise_id' => '127',
                        'title' => '韩国',
                    ),
                    '4' => array(
                        'id' => 10,
                        'parent_id' => '5',
                        'enterprise_id' => '127',
                        'title' => '越南',
                    ),
                    '5' => array(
                        'id' => 11,
                        'parent_id' => '5',
                        'enterprise_id' => '127',
                        'title' => '泰国',
                    ),
                    '6' => array(
                        'id' => 12,
                        'parent_id' => '5',
                        'enterprise_id' => '127',
                        'title' => '美国',
                    ),
                )
            ),
        );*/
    }

    public function pieceTags($tags){
        $tagsArr = array();
        foreach($tags as $k => $tag_cls){
            $tagsArr[$k]['id'] = $tag_cls->getId();
            $tagsArr[$k]['parent_id'] = $tag_cls->getParentId();
            $tagsArr[$k]['enterprise_id'] = $tag_cls->getEnterpriseId();
            $tagsArr[$k]['title'] = $tag_cls->getTitle();
            if($tag_cls->getChildTags()){
                $tagsArr[$k]['child_tag'] = $this->pieceTags($tag_cls->getChildTags());
            }
        }
        return $tagsArr;
    }
}